<!doctype>
<html>

<head>
	<title>快捷菜单</title>
	<script src="https://lib.baomitu.com/jquery/3.3.1/jquery.min.js"></script>
	<script src="https://apps.bdimg.com/libs/lodash/3.5.0/lodash.js"></script>
	<style>
		.menu-item {
			padding-right: 20px;
		}
	</style>
</head>

<body>
	<div id="menu-container">

	</div>
	<div id="menu-render-template" style="display:none;">
		<div>
			<fieldset>
				<legend data-menu-id='{menu_id}'>{parent_menu_name}</legend>

			</fieldset>
		</div>

	</div>
</body>

</html>
<script>

	//菜单接口地址
	var menuApiPath = '/Base/Common/Menu/GetMenuTreeOriginal';
	//var menuApiPath='/Base/Common/Menu/GetNewUserMenuTreeOriginal';

	if(top.location.host.indexOf('scm.3songshu.com')>-1){
		//menuApiPath='/Base/Common/Menu/GetNewUserMenuTreeOriginal';
	}

	//忽略的菜单
	var ignoreMenus=[662, 819, 19, 148];
	var ignoreMenuMap={};

	(function(){
		ignoreMenus.forEach(d=>{
			ignoreMenuMap[d]=1;
		});

	})();

	var menuList = [{
		menu_name: 'WMS',
		menu_path: '',//空为目录，非空为菜单
		children: [
			{
				menu_name: '补货参数设置',
				menu_path: 'view.OMS.buhou_params_settings',
			}, {
				menu_name: '自营调拨',
				children: [{
					menu_name: '自营调拨数据上传',
					menu_path: 'view.WMS.stock_info.proprietary_aupload',
				}, {
					menu_name: '自营调拨数据核算',
					menu_path: 'view.WMS.stock_info.proprietary_pccounting',
				},]
			}, {
				menu_name: '区域物流',
				children: [
					{ menu_name: '区域物流设置', menu_path: 'view.OMS.area_logistic_index.index' },
					{ menu_name: '产品指定发货仓库设置', menu_path: 'view.BaseSystem.wms.area_products_settings.index' },
				]

			},
			{ menu_name: '拣货单打印', menu_path: 'view.WMS.stock_info.sort_order_printing' },
			{
				menu_name: '调拨', children: [
					{ menu_name: '调拨计划', menu_path: 'view.WMS.trans_task_new.index' },
				]
			}
		]

	}, {
		menu_name: '特种部队',
		children: [{
			menu_name: '平台产品关联设置', menu_path: 'view.BaseSystem.products_id_relations',
		}]

	}];

	$(function () {
		var menuRenderTemplate = $('#menu-render-template').html();
		function renderItemMenu(container, parent_menu, children_menu) {
			if (!parent_menu.menu_path) {
				var str = menuRenderTemplate.replace('{parent_menu_name}', parent_menu.menu_name).replace('{menu_id}',parent_menu.id);
				var realContainer = container;
				if (realContainer.length <= 0) {
					realContainer = container;
				}
				var jq = $(str).appendTo(realContainer);
				(function (jq) {
					(children_menu || []).forEach(function (child) {
						renderItemMenu(jq, child, child.children);
					});
				})(jq.find('fieldset'));
			} else {
				var str = `<a href="${parent_menu.menu_path}" class="menu-item" data-menu-id="${parent_menu.id}">${parent_menu.menu_name}</a>`;
				var jq = $(str).appendTo(container);
				(function (jq) {
					(children_menu || []).forEach(function (child) {
						renderItemMenu(jq.find('fieldset'), child, child.children);
					});
				})(jq.closest('fieldset'));
			}
		}

		function renderMenu(menuList) {
			var container = $('#menu-container');
			menuList.forEach(item => {
				renderItemMenu(container, item, item.children);
			});
		}

		//renderMenu(menuList);

		$(document).on('click', '.menu-item', function (evt) {
			evt.preventDefault();
			var jq = $(this);
			parent.open_new_if.show(jq.text(), jq.attr('href'));
			$(document).trigger('close');
			return false;
		});

		//获取菜单数据
		parent.sha_tools_panel.ajax_get(menuApiPath, null, d => {
			var data = { id: 0, menu_name: '菜单', };
			toParentChildrenData(d.data, data);
			renderMenu([data]);
		});

		//转换成{xx,children:xx}格式
		function toParentChildrenData(data, parent) {
			var ret = [];
			if (_.any(data, d => d.son_rowid == parent.id)) {
				_.filter(data, d => d.son_rowid == parent.id).forEach(d => {
					if(!ignoreMenuMap[d.rowid]){
						var item = { id: d.rowid, menu_name: d.system_name, menu_path: d.menu_path };
						toParentChildrenData(data, item);
						ret.push(item);
					}
					
				});
			}
			parent.children = ret;
			return ret;
		}

	});
</script>